/**
 * @author Thomas Liu
 */

package frontend;

import java.awt.Dimension;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JCheckBox;

import javax.swing.WindowConstants;
import javax.swing.border.LineBorder;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.ListModel;
import javax.swing.ListSelectionModel;
import javax.swing.SpinnerListModel;
import javax.swing.SpinnerNumberModel;

import dbmanager.FoodManager;

import framework.FoodItem;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class FoodItemGUI extends javax.swing.JPanel {

	{
		//Set Look & Feel
		try {
			javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName());
		} catch(Exception e) {
			e.printStackTrace();
		}
	}

	private JList foodItemList;
	private JScrollPane jScrollPane1;
	private JScrollPane jScrollPane2;
	private JSpinner removeAmountSpinner;
	private JSpinner addAmountSpinner;
	private JTextField fridgeFilterField;
	private JTextField foodFilterField;
	private JLabel foodLabel;
	private JLabel refrigeratorLabel;
	private JButton removeButton;
	private JButton addButton;
	private JLabel viewLabel;
	private JCheckBox prefsBox;
	private JList fridgeList;

	/**
	* Auto-generated main method to display this 
	* JPanel inside a new JFrame.
	*/
	public static void main(String[] args) {
		JFrame frame = new JFrame();
		frame.getContentPane().add(new FoodItemGUI());
		frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		frame.pack();
		frame.setVisible(true);
	}
	
	public FoodItemGUI() {
		super();
		initGUI();
	}
	
	private void initGUI() {
		try {
			this.setPreferredSize(new java.awt.Dimension(968, 734));
			this.setMinimumSize(new java.awt.Dimension(1024, 768));
			this.setVisible(false);
			this.setSize(968, 734);
			this.setBackground(new java.awt.Color(255,255,255));
			this.setLayout(null);
			{
				jScrollPane1 = new JScrollPane();
				this.add(jScrollPane1);
				jScrollPane1.setBounds(39, 78, 249, 538);
				{
					ListModel foodItemListModel = 
						new DefaultComboBoxModel(
								new String[] { "Item One", "Item Two" });
					foodItemList = new JList();
					jScrollPane1.setViewportView(foodItemList);
					foodItemList.setModel(foodItemListModel);
					getFoodItemsLike("%");
					foodItemList.setBounds(39, 78, 217, 31);
					foodItemList.setBorder(BorderFactory.createTitledBorder(""));
					foodItemList.setBackground(new java.awt.Color(248,238,235));
					foodItemList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
				}
			}
			{
				jScrollPane2 = new JScrollPane();
				this.add(jScrollPane2);
				jScrollPane2.setBounds(552, 78, 375, 538);
				{
					ListModel fridgeListModel = 
						new DefaultComboBoxModel(
								new String[] { "Item One", "Item Two" });
					fridgeList = new JList();
					jScrollPane2.setViewportView(fridgeList);
					fridgeList.setModel(fridgeListModel);
					getFridgeItemsLike("%");
					fridgeList.setBounds(552, 78, 375, 577);
					fridgeList.setBackground(new java.awt.Color(224,237,246));
					fridgeList.setBorder(BorderFactory.createTitledBorder(""));
					fridgeList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
					fridgeList.addMouseListener(new MouseAdapter() {
						public void mouseClicked(MouseEvent evt) {
							try {
								fridgeListMouseClicked(evt);
							} catch (SQLException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}
					});
				}
			}
			{
				prefsBox = new JCheckBox();
				this.add(prefsBox);
				prefsBox.setText("Ingredients suiting my preferences");
				prefsBox.setBounds(48, 655, 251, 28);
				prefsBox.setBackground(new java.awt.Color(255,255,255));
			}
			{
				viewLabel = new JLabel();
				this.add(viewLabel);
				viewLabel.setText("View Only:");
				viewLabel.setBounds(38, 628, 80, 27);
				viewLabel.setFont(new java.awt.Font("AlArabiya",1,12));
			}
			{
				addButton = new JButton();
				this.add(addButton);
				addButton.setText("Add to My Refrigerator ->");
				addButton.setBounds(299, 248, 241, 37);
				addButton.addMouseListener(new MouseAdapter() {
					public void mouseClicked(MouseEvent evt) {
						try {
							addButtonMouseClicked(evt);
						} catch (SQLException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				});
			}
			{
				removeButton = new JButton();
				this.add(removeButton);
				removeButton.setText("<- Remove from My Refrigerator");
				removeButton.setBounds(299, 388, 241, 37);
				removeButton.addMouseListener(new MouseAdapter() {
					public void mouseClicked(MouseEvent evt) {
						try {
							removeButtonMouseClicked(evt);
						} catch (SQLException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				});
			}
			{
				refrigeratorLabel = new JLabel();
				this.add(refrigeratorLabel);
				refrigeratorLabel.setText("My Refrigerator");
				refrigeratorLabel.setBounds(552, 18, 156, 27);
				refrigeratorLabel.setFont(new java.awt.Font("Bitstream Vera Sans",1,14));
			}
			{
				foodLabel = new JLabel();
				this.add(foodLabel);
				foodLabel.setText("Available Food Items");
				foodLabel.setBounds(39, 18, 174, 27);
				foodLabel.setFont(new java.awt.Font("Bitstream Vera Sans",1,14));
			}
			{
				foodFilterField = new JTextField();
				this.add(foodFilterField);
				foodFilterField.setBounds(39, 45, 249, 27);
				foodFilterField.addKeyListener(new KeyAdapter() {
					public void keyPressed(KeyEvent evt) {
						try {
							foodFilterFieldKeyPressed(evt);
						} catch (SQLException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
					
				});
			}
			{
				fridgeFilterField = new JTextField();
				this.add(fridgeFilterField);
				fridgeFilterField.setBounds(552, 45, 375, 27);
				fridgeFilterField.addKeyListener(new KeyAdapter() {
					public void keyPressed(KeyEvent evt) {
						try {
							fridgeFilterFieldKeyTyped(evt);
						} catch (SQLException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				});
			}
			{
				SpinnerNumberModel addAmountSpinnerModel = 
					new SpinnerNumberModel(1, 1, Integer.MAX_VALUE, 1);
				addAmountSpinner = new JSpinner();
				this.add(addAmountSpinner);
				addAmountSpinner.setModel(addAmountSpinnerModel);
				addAmountSpinner.setBounds(299, 210, 121, 26);
			}
			{
				SpinnerNumberModel removeAmountSpinnerModel = 
					new SpinnerNumberModel(1, 1, 1, 1);
				removeAmountSpinner = new JSpinner();
				this.add(removeAmountSpinner);
				removeAmountSpinner.setModel(removeAmountSpinnerModel);
				removeAmountSpinner.setBounds(427, 350, 113, 26);
				removeAmountSpinner.setEnabled(false);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void getFoodItemsLike(String like) throws SQLException{
		updateFoodItemList(FoodManager.fetchFoodItems(like, true));
	}
	
	private void updateFoodItemList(ArrayList <FoodItem> foodList){
		String[] modelList = new String[foodList.size()];
		for(int i=0; i<foodList.size(); i++){
			modelList[i] = foodList.get(i).getName();
		}
		foodItemList.setModel(new DefaultComboBoxModel(modelList));
	}
	
	private void getFridgeItemsLike(String like) throws SQLException{
		updateFridgeList(FoodManager.fetchFridgeItems(like));
	}
	
	private void updateFridgeList(ArrayList <FoodItem> foodList) throws SQLException{
		String[] modelList = new String[foodList.size()];
		for(int i=0; i<foodList.size(); i++){
			String name = foodList.get(i).getName();
			String unit = foodList.get(i).getUnit();
			modelList[i] = "<html>"+FoodManager.getAmount(name)+ " "+unit+"<b> - "+name;
		}
		fridgeList.setModel(new DefaultComboBoxModel(modelList));
	}
	
	
	private void fridgeFilterFieldKeyTyped(KeyEvent evt) throws SQLException {
		getFridgeItemsLike(fridgeFilterField.getText());
		//TODO add your code for fridgeFilterField.keyTyped
	}
	
	private void addButtonMouseClicked(MouseEvent evt) throws SQLException {
		FoodManager.addPersonalFoodItem((String)foodItemList.getSelectedValue(), (Integer)addAmountSpinner.getValue());
		getFridgeItemsLike(fridgeFilterField.getText());
		//TODO add your code for addButton.mouseClicked
	}
	
	private void removeButtonMouseClicked(MouseEvent evt) throws SQLException {
		String food = ((String)fridgeList.getSelectedValue()).split("-")[1].substring(1);
		FoodManager.removePersonalFoodItem(food, (Integer)removeAmountSpinner.getValue());
		getFridgeItemsLike(fridgeFilterField.getText());
		//TODO add your code for removeButton.mouseClicked
	}
	
	private void foodFilterFieldKeyPressed(KeyEvent evt) throws SQLException {
		getFoodItemsLike(foodFilterField.getText());
	}
	
	private void fridgeListMouseClicked(MouseEvent evt) throws SQLException {
		if(fridgeList.getSelectedValue() == null)
			removeAmountSpinner.setEnabled(false);
		int max = FoodManager.getAmount(((String)fridgeList.getSelectedValue()).split("-")[1].substring(1));
		
		SpinnerNumberModel removeAmountSpinnerModel = 
			new SpinnerNumberModel(max, 0, max, 1);
		removeAmountSpinner.setModel(removeAmountSpinnerModel);
		removeAmountSpinner.setEnabled(true);
	}

}
